﻿using StudentsMngDemo.Models;
using StudentsMngDemo.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace StudentsMngDemo
{
    public partial class Form_Main : Form
    {
        private readonly SdtMngDbContext sdtMngDbContext;
        /// <summary>
        /// 主窗口
        /// </summary>
        public Form_Main(SdtMngDbContext sdtMngDbContext)
        {
            InitializeComponent();
            this.sdtMngDbContext = sdtMngDbContext;
        }

        private void button_Commit_Click(object sender, EventArgs e)
        {
            Random random = new Random();
            Student student = new Student()
            {
                StudentCode = random.Next(10000, 99999),
                StudentName = textBox_StudentName.Text,
                StudentGrade = comboBox_StudentGrade.Text,
                StudentClass = comboBox_StudentClass.Text,
                BirthTime = Convert.ToDateTime(dateTimePicker_BirthTime.Text),
                Sex = radioButton_Man.Checked ? 1 : 0,
                Age = Convert.ToInt32(numericUpDown_Age.Value),
                Tel = maskedTextBox_Tel.Text,
                Address = richTextBox_Address.Text
            };
            IStudentDataOperation studentDataOperation = new StudentDataOperation(sdtMngDbContext);
            if (studentDataOperation.AddStudent(student))
            {
                MessageBox.Show("保存成功","系统提示", MessageBoxButtons.OK, MessageBoxIcon.Question);
            }
            else
            {
                MessageBox.Show("保存失败","系统提示",  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
